package com.netmi.myvideopicker;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.netmi.myvideopicker.adapter.VideoFolderAdapter;
import com.netmi.myvideopicker.adapter.VideoRecyclerAdapter;
import com.netmi.myvideopicker.bean.VideoItem;
import com.netmi.myvideopicker.data.VideoDataSource;
import com.netmi.myvideopicker.data.VideoFolder;
import com.netmi.myvideopicker.data.VideoPicker;
import com.netmi.myvideopicker.databinding.ActivitySelectVideoBinding;
import com.netmi.myvideopicker.utils.JumpActivityUtils;
import com.netmi.myvideopicker.view.FolderPopUpWindow;

import java.util.ArrayList;
import java.util.List;

public class SelectVideoActivity extends AppCompatActivity implements View.OnClickListener, VideoDataSource.OnVideosLoadedListener, VideoRecyclerAdapter.OnImageItemClickListener {

    private final String TAG = SelectVideoActivity.class.getSimpleName();
    private Button btSelectVideo;
    private VideoRecyclerAdapter adapter;
    private RecyclerView recyclerView;
    private List<VideoItem> videoItemList = new ArrayList<>();

    private ActivitySelectVideoBinding mBinding;

    private List<VideoFolder> videoFolders = new ArrayList<>();
    //    private static int videoFolderPosition; //选择了哪个文件夹
    private VideoFolderAdapter videoFolderAdapter;
    private FolderPopUpWindow mFolderPopupWindow;
//    private VideoPicker videoPicker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_select_video);

//        videoPicker = VideoPicker.getInstance();
        new VideoDataSource(this, null, this);

        adapter = new VideoRecyclerAdapter(this, videoItemList, this);
        mBinding.recycler.setLayoutManager(new GridLayoutManager(this, 3));
        mBinding.recycler.setAdapter(adapter);

        videoFolderAdapter = new VideoFolderAdapter(this, videoFolders);
    }

    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.tv_dir) {
            if (videoFolders.size() == 0) {
                Log.i("ImageGridActivity", "您的手机没有图片");
                videoFolders = VideoPicker.getInstance().getVideoFolders();
            }
            //点击文件夹按钮
            createPopupFolderList();
            videoFolderAdapter.refreshData(videoFolders);  //刷新数据
            mFolderPopupWindow.showAtLocation(mBinding.footerBar, Gravity.NO_GRAVITY, 0, 0);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    /**
     * 创建弹出的ListView
     */
    private void createPopupFolderList() {
        mFolderPopupWindow = new FolderPopUpWindow(this, videoFolderAdapter);
        mFolderPopupWindow.setOnItemClickListener(new FolderPopUpWindow.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                videoFolderAdapter.setSelectIndex(position);
//                imagePicker.setCurrentImageFolderPosition(position);
                mFolderPopupWindow.dismiss();
                VideoFolder videoFolder = (VideoFolder) adapterView.getAdapter().getItem(position);
                if (null != videoFolder) {
//                    mImageGridAdapter.refreshData(imageFolder.images);
                    VideoPicker.getInstance().setCurrentVideoFolderPosition(position);
//                    videoFolderPosition = position;
                    adapter.refreshData(videoFolder.videos);
                    mBinding.tvDir.setText(videoFolder.name);
                }
            }
        });
//        mFolderPopupWindow.setMargin(mFooterBar.getHeight());
    }

    @Override
    public void onVideosLoaded(List<VideoFolder> videoFolders) {
        if (videoFolders != null && videoFolders.size() > 0) {
            this.videoFolders = videoFolders;
            videoItemList = videoFolders.get(VideoPicker.getInstance().getCurrentVideoFolderPosition()).videos;
            VideoPicker.getInstance().setVideoFolders(videoFolders);
        }
//        for (int i =0;i<videoFolders.size();i++){
//            Log.d(TAG, "tgw 文件夹onVideosLoaded: 文件夹--"+videoFolders.get(i).name);
//            Log.d(TAG, "------------------");
//            for (int j=0;j<videoFolders.get(i).videos.size();j++){
//                Log.d(TAG, "tgw 文件夹onVideosLoaded:--"+videoFolders.get(i).videos.get(j).path);
//            }
//        }
//        adapter.notifyDataSetChanged();
        adapter.refreshData(videoItemList);
    }

    @Override
    public void onImageItemClick(View view, VideoItem imageItem, int position) {
        if (view.getId() == R.id.checkView) {
            //点击勾选了
            VideoItem videoItem = new VideoItem();
            videoItem = videoItemList.get(position);
            if (videoItemList.get(position).isSelect) {
                videoItem.isSelect = false;
            } else {
                videoItem.isSelect = true;
            }
            videoItemList.set(position, videoItem);

            adapter.refreshData(videoItemList);
            int selectNum = 0;
            for (int i = 0; i < videoItemList.size(); i++) {
                if (videoItemList.get(i).isSelect) {
                    selectNum++;
                }
            }
            mBinding.setSelectNum(selectNum + "");

        } else if (view.getId() == R.id.iv_thumb) {
            if (!TextUtils.isEmpty(imageItem.path)) {

                if (videoFolders.size() == 0) {
                    Log.i("ImageGridActivity", "您的手机没有图片");
                    videoFolders = VideoPicker.getInstance().getVideoFolders();
                }
                Intent intent = new Intent(SelectVideoActivity.this, PlayVideoActivity.class);
//                intent.putExtra("videoPath", imageItem.path);
                Bundle bundle = new Bundle();
                //哪个文件夹
                bundle.putSerializable("videoFolder", videoFolders.get(VideoPicker.getInstance().getCurrentVideoFolderPosition()).videos);
                //文件夹下的哪个视频
                bundle.putInt("videoPosition", position);
                intent.putExtras(bundle);
                startActivityForResult(intent, PlayVideoActivity.PLAY_REQUEST);
            } else {
                Toast.makeText(SelectVideoActivity.this, "该视频无效，请另外选择", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PlayVideoActivity.PLAY_REQUEST) {
//            adapter.refreshData(videoFolders.get(videoFolderPosition).videos);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
